﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

using Bussiness_Objects;

namespace DataObjects.ADO
{
    public class SqlPhieuMuonSach : IPhieuMuonSachDao
    {
        public DataTable GetTable(object businessData)
        {
            PhieuMuonSach _sach = businessData as PhieuMuonSach;
            if (_sach != null)
            {
                _sach.SetParameterSelect();
                string sqlString = "sp_PHIEUMUONSACH_selectAll";
                return Db.Read(sqlString, _sach.parms);
            }
            return null;
        }

        public DataTable GetByMaDG(object businessData)
        {
            PhieuMuonSach _sach = businessData as PhieuMuonSach;

            if (_sach != null)
            {
                _sach.SetParameterSelectID();
                string sqlString = "sp_PHIEUMUONSACH_selectByID";
                return Db.Read(sqlString, _sach.parms);
            }
            return null;
        }

        public DataTable GetById(object businessData)
        {
            PhieuMuonSach _sach = businessData as PhieuMuonSach;

            if (_sach != null)
            {
                _sach.SetParameterSelectID();
                string sqlString = "sp_PHIEUMUONSACH_selectByID";
                return Db.Read(sqlString, _sach.parms);
            }
            return null;
        }

        public DataTable GetCTMuon(object businessData,string ngayTra)
        {
            PhieuMuonSach _sach = businessData as PhieuMuonSach;
            if (_sach != null)
            {
                _sach.SetParamterGetCTMuon(ngayTra);
                string sqlString = "sp_PHIEUMUONSACH_selectCTMuon";
                return Db.Read(sqlString, _sach.parms);
            }
            return null;
        }

        public string Insert(object businessData)
        {
            PhieuMuonSach _sach = businessData as PhieuMuonSach;

            if (_sach != null)
            {
                _sach.SetParameterInsert();
                string sqlString = "sp_PHIEUMUONSACH_insert";
                string result =  Db.ExecuteStoreProcedure(sqlString, _sach.parms);
                _sach.SoPM = (int)((SqlParameter)_sach.parms[3]).Value;
                return result;
            }
            return "Error";
        }

        public string Update(object businessData)
        {
            PhieuMuonSach _sach = businessData as PhieuMuonSach;

            if (_sach != null)
            {
                _sach.SetParameterUpdate();
                string sqlString = "sp_PHIEUMUONSACH_update";
                return Db.ExecuteStoreProcedure(sqlString, _sach.parms);
            }
            return "Error";
        }

        public string Delete(object businessData)
        {
            PhieuMuonSach _sach = businessData as PhieuMuonSach;
            if (_sach != null)
            {
                _sach.SetParameterDelete();
                string sqlString = "sp_PHIEUMUONSACH_delete";
                return Db.ExecuteStoreProcedure(sqlString, _sach.parms);
            }
            return "Error";
        }
    }
}
